import { cloneDeep } from 'lodash'
import { getRole } from '@/utils'
const filerMenu = (menus) => {
  const role = getRole()
  const authMenus = []
  menus.forEach(menu => {
    if (menu.roles.includes(role) || menu.roles.includes('*')) {
      // 判断是否有children
      if (menu.children) {
        menu.children = filerMenu(menu.children)
      }
      authMenus.push(menu)
    }
  })
  return authMenus
}
export default {
  namespaced: true,
  state: {
    token: localStorage.getItem('token') || '',
    role: localStorage.getItem('role') || '',
    userInfo: localStorage.getItem('userInfo')
      ? JSON.parse(localStorage.getItem('userInfo'))
      : {},
    // 保存当前所有侧边菜单数组
    menus: [
      {
        label: '仪表盘',
        path: '/dashBoard',
        icon: 'Compass',
        roles: ['*']
      },
      {
        label: '商品管理',
        path: 'item',
        icon: 'Compass',
        roles: ['superAdmin', 'admin', 'a', 'b'],
        children: [
          {
            label: '商品列表',
            path: '/itemLists',
            roles: ['superAdmin', 'admin', 'a', 'b']
          },
          {
            label: '商品分类列表',
            path: '/cateLists',
            roles: ['superAdmin', 'admin', 'a', 'b']
          }
        ]
      },
      {
        label: '个人中心',
        path: '/userInfo',
        icon: 'User',
        roles: ['superAdmin', 'admin', 'a', 'b']
      },
      {
        label: '设置',
        path: '/setting',
        icon: 'Setting',
        roles: ['superAdmin', 'a', 'b']
      }
    ]
  },
  getters: {
    authMenus (state) {
      const menus = cloneDeep(state.menus)
      return filerMenu(menus)
    }
  },
  mutations: {
    INIT_USER (state, { token, role, userInfo }) {
      state.token = token
      state.role = role
      state.userInfo = userInfo
    }
  }
}
